<template>
  <Form
    ref="loginForm"
    :model="form"
    :rules="rules"
    @keydown.enter.native="handleSubmit"
  >
    <FormItem prop="name">
      <Input v-model="form.name" placeholder="请输入登陆名">
        <span slot="prepend">
          <Icon :size="16" type="ios-person"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="userName" v-if="showRegis">
      <Input v-model="form.userName" placeholder="请输入用户名">
        <span slot="prepend">
          <Icon :size="16" type="ios-person"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="password">
      <Input type="password" v-model="form.password" placeholder="请输入密码">
        <span slot="prepend">
          <Icon :size="14" type="md-lock"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="password2" v-if="showRegis">
      <Input
        type="password"
        v-model="form.password2"
        placeholder="请输入确认密码"
      >
        <span slot="prepend">
          <Icon :size="14" type="md-lock"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem>
      <Button @click="handleSubmit" type="primary" long>{{
        showRegis ? "确定" : "登陆"
      }}</Button>
      <Button @click="handleShowRegis" long style="margin-top:10px">{{
        showRegis ? "返回登陆" : "注册"
      }}</Button>
    </FormItem>
  </Form>
</template>
<script>
export default {
  name: "LoginForm",
  props: {
    userNameRules: {
      type: Array,
      default: () => {
        return [{ required: true, message: "账号不能为空", trigger: "blur" }];
      },
    },
    passwordRules: {
      type: Array,
      default: () => {
        return [{ required: true, message: "密码不能为空", trigger: "blur" }];
      },
    },
  },
  data() {
    return {
      showRegis: false,
      form: {
        name: "user",
        userName: "",
        password: "pass123",
        password2: "",
      },
    };
  },
  computed: {
    rules() {
      const validatePassCheck = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请再次输入密码"));
        } else if (value !== this.form.password) {
          callback(new Error("两次密码不一致！"));
        } else {
          callback();
        }
      };
      return {
        name: this.showRegis
          ? [{ required: true, message: "用户名不能为空", trigger: "blur" }]
          : [],
        userName: this.userNameRules,
        password: this.passwordRules,
        password2: [{ validator: validatePassCheck, trigger: "blur" }],
      };
    },
  },
  methods: {
    handleShowRegis() {
      this.showRegis = !this.showRegis;
      if (this.showRegis) {
        this.form.name = "";
        this.form.password = "";
      } else {
        this.form.name = "user";
        this.form.password = "pass123";
      }
    },
    handleSubmit() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$emit("on-success-valid", {
            name: this.form.name,
            userName: this.form.userName,
            password: this.form.password,
            password2: this.form.password2,
            showRegis: this.showRegis,
          });
        }
      });
    },
  },
};
</script>
